#include "Track.h"

QVector<QString> Track::columns(7);
int Track::lastId=-1;

Track::Track() : QObject()
{
	path="";
	artist="Unknown";
	title="Unknown";
	length=0;
	album="Unknown";
	trackNumber=-1;
	genre="Unknown";
	mediaResolver=NULL;
	resolved=false;
	r_metaData=false;
	r_time=false;
	fillColumns();
	id=++lastId;
	
}

Track::Track(QString path) : QObject()
{
	this->path=path;
	artist="Unknown";
	title="Unknown";
	length=0;
	album="Unknown";
	trackNumber=-1;
	genre="Unknown";
	mediaResolver=NULL;
	resolved=false;
	r_metaData=false;
	r_time=false;
	fillColumns();
	id=++lastId;
}

Track::Track(QString path, QString artist, QString title, int length, QString album, int trackNumber, QString genre) : QObject()
{
	this->path=path;
	this->artist=artist;
	this->title=title;
	this->length=length;
	this->album=album;
	this->trackNumber=trackNumber;
	this->genre=genre;
	mediaResolver=NULL;
	resolved=true;
	r_metaData=false;
	r_time=false;
	fillColumns();
	id=++lastId;
}

void Track::fillColumns() 
{
	if(columns[0]=="")
	{
		columns[0]=(tr("Track")); // trackNumber 
		columns[1]=(tr("Title")); // title
		columns[2]=(tr("Artist")); // artist
		columns[3]=(tr("Album")); // album
		columns[4]=(tr("Genre")); // genre
		columns[5]=(tr("Time")); // length
		columns[6]=(tr("Location")); // path
	}
}

// getters

QString Track::getPath() const
{
	return path;
}

QString Track::getArtist() const
{
	return artist;
}

QString Track::getTitle() const
{
	return title;
}

qint64 Track::getLength() const
{
	return length;
}

QString Track::displayLength() const
{
	 QTime displayTime(0, (length/60000) % 60, (length/1000) % 60);
	 return displayTime.toString("mm:ss");
}
	

QString Track::getAlbum() const
{
	return album;
}

bool Track::isResolved()
{
	return resolved;
}

int Track::getTrackNumber() const
{
	return trackNumber;
}

QString Track::getGenre() const
{
	return genre;
}

int Track::getId() const
{
	return id;
}

// setters

void Track::setPath(QString path)
{
	this->path=path;
}

void Track::setArtist(QString artist)
{
	this->artist=artist;
}

void Track::setTitle(QString title)
{
	this->title=title;
}

void Track::setLength(qint64 length)
{
	this->length=length;
}

void Track::setAlbum(QString album)
{
	this->album=album;
}

void Track::setTrackNumber(int trackNumber)
{
	this->trackNumber=trackNumber;
}

void Track::setGenre(QString genre)
{
	this->genre=genre;
}

void Track::setLastId(int id)
{
	lastId=id;
}

void Track::setResolved(bool resolved)
{
	this->resolved=resolved;
}

/* add the track to the resolver and connect the resolver to onMediaResolverMetaDataChanged(): 
when the resolver will resolve the track, this function will be called */
void Track::resolve()
{
	resolved=false;
	mediaResolver=new Phonon::MediaObject(NULL);
	mediaResolver->setCurrentSource(Phonon::MediaSource(path));
	connect(mediaResolver, SIGNAL(metaDataChanged()), this, SLOT(onMediaResolverMetaDataChanged()));
	connect(mediaResolver, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(onMediaResolverStateChanged(Phonon::State, Phonon::State)));
}

/* fill the track's fields with the resolver meta data */	
void Track::onMediaResolverMetaDataChanged()
{
		QMap<QString, QString> metaData=mediaResolver->metaData(); // getting the meta data

		// for the artist, getting the value from the QMap metaData
		artist=metaData.value("ARTIST");
		
		title=metaData.value("TITLE");
		album=metaData.value("ALBUM");
		genre=metaData.value("GENRE");
		
		bool ok=false;
		trackNumber=metaData.value("TRACK-NUMBER").toInt(&ok, 10);
		if(!ok)
		{
			trackNumber=-2;
		}
		
		
		r_metaData=true;
		if(r_time==true)
		{
			resolved=true;
			emit(metaDataResolved(id));
		}
}

void Track::onMediaResolverStateChanged(Phonon::State newState, Phonon::State)
{
	if(newState!=Phonon::ErrorState)
	{
		length=mediaResolver->totalTime();
		r_time=true;
		if(r_metaData==true)
		{
			resolved=true;
			emit(metaDataResolved(id));
		}
	}
}
	

bool Track::operator== (const Track & other)
{
	return path==other.getPath();
}

QVariant Track::operator[](int off)
{
	switch(off)
	{
		case 0:
		return trackNumber;
		case 1:
		return title;
		case 2:
		return artist;
		case 3:
		return album;
		case 4: 
		return genre;
		case 5:
		return displayLength();
		case 6:
		return path;
		default:
		return QVariant();		
	}
}
	
	

		
		
